package com.hs.pre.sys.user;


import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hs.pre.model.UserBean;
import com.hs.pre.module.sort.SortDAO;
import com.hs.pre.page.PageParam;
import com.hs.pre.page.PageUtil;

public class UserAction  extends org.apache.struts.actions.DispatchAction{

	
	/**
	 * 添加用户
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward addUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		CreateUserBean createUserBean = new CreateUserBean(request);
		UserBean userBean = createUserBean.getUserBean();
		if(userBean.getLoginId() != null && userBean.getPwd() != null && userBean.getUserName() != null)
		{
			UserDAO userDAO = new UserDAO();
			try {
				userDAO.addUser(userBean);
				
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return new ActionForward(mapping.findForward("relist").getPath(),true);
	}
	
	
	/**
	 * 删除用户
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward delUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		String[] userIdTem= request.getParameterValues("userId");
		if(userIdTem != null)
		{
			List list = new ArrayList();
			int size = userIdTem.length;
			for(int i=0 ; i<size;i++)
			{
				Object[] o = new Object[1];
				o[0] = Long.parseLong(userIdTem[i]);
				list.add(o);
			}
			UserDAO userDAO = new UserDAO();
			userDAO.delUser(list);
		}
		return new ActionForward(mapping.findForward("relist").getPath(),true);
	}
	
	
	/**
	 *  修改用户
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward editUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		
		CreateUserBean createUserBean = new CreateUserBean(request);
		UserBean userBean = createUserBean.getUserBean();
		
		if(userBean.getUserId() != null && userBean.getLoginId() != null && userBean.getPwd() != null && userBean.getUserName() != null)
		{
			UserDAO userDAO = new UserDAO();
			try {
				userDAO.editUser(userBean);
				
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return new ActionForward(mapping.findForward("relist").getPath(),true);
	}
	
	/**
	 *  用户列表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward listUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		 PageParam pageParam = new PageParam(request);
		 pageParam.setBasePath("/pre/user.do?userDis=listUser");
		 /**
		  * 设置 显示个数
		  */
		 pageParam.setPageSize(15);
		PageUtil pageUtil = new UserDAO().getUserInfoList(pageParam, request.getParameter("userName"));
		request.setAttribute("pageUtil", pageUtil);
		return mapping.findForward("listUser");
	}
	
	/**
	 *  用户基本信息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward userView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		String userId = request.getParameter("userId");
		
		if(userId != null && !(userId=userId.trim()).equals(""))
		{
			Integer.parseInt(userId);
			UserDAO userDAO= new UserDAO();
			try {
				UserBean userBean = userDAO.findBean(userId);
				if(userBean == null)
					return null;
				request.setAttribute("userBean", userBean);
				return mapping.findForward("edit");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return null;
	}
}
